package com.zangs.emqx_expand.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.zangs.emqx_expand.common.utils.DateUtils;
import com.zangs.emqx_expand.common.utils.StringUtils;
import com.zangs.emqx_expand.common.utils.uuid.IdUtils;
import com.zangs.emqx_expand.system.domain.AppUserApi;
import com.zangs.emqx_expand.system.domain.SysUserRole;
import com.zangs.emqx_expand.system.mapper.AppUserApiMapper;
import com.zangs.emqx_expand.system.service.IAppUserApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zangs.emqx_expand.system.mapper.AppUserMapper;
import com.zangs.emqx_expand.system.domain.AppUser;
import com.zangs.emqx_expand.system.service.IAppUserService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 接口密钥管理Service业务层处理
 * 
 * 
 * @since 2025-01-20
 */
@Service
public class AppUserServiceImpl implements IAppUserService 
{
    @Autowired
    private AppUserMapper appUserMapper;
    @Autowired
    private AppUserApiMapper appUserApiMapper;

    /**
     * 查询接口密钥管理
     * 
     * @param appUserId 接口密钥管理主键
     * @return 接口密钥管理
     */
    @Override
    public AppUser selectAppUserByAppUserId(Long appUserId)
    {
        return appUserMapper.selectAppUserByAppUserId(appUserId);
    }
    /**
     * 查询接口密钥管理
     * @param appId 查询条件
     * @return 结果包含密钥
     */
    @Override
    public AppUser selectAppUserByAppId(String appId)
    {
        return appUserMapper.selectAppUserByAppId(appId);
    }
    /**
     * 查询接口密钥管理列表
     * 
     * @param appUser 接口密钥管理
     * @return 接口密钥管理
     */
    @Override
    public List<AppUser> selectAppUserList(AppUser appUser)
    {
        return appUserMapper.selectAppUserList(appUser);
    }

    @Override
    public AppUser selectAppUserByAppIdAndPath(String appId, String path) {

        return   appUserMapper. selectAppUserByAppIdAndPath(appId,path);
    }

    /**
     * 新增接口密钥管理
     * 
     * @param appUser 接口密钥管理
     * @return 结果
     */
    @Override
    public int insertAppUser(AppUser appUser)
    {
        appUser.setCreateTime(DateUtils.getNowDate());
        appUser.setAppId(IdUtils.simpleUUID());
        appUser.setAppSecret(IdUtils.simpleUUID());
        return appUserMapper.insertAppUser(appUser);
    }

    /**
     * 修改接口密钥管理
     * 
     * @param appUser 接口密钥管理
     * @return 结果
     */
    @Override
    public int updateAppUser(AppUser appUser)
    {
        appUser.setUpdateTime(DateUtils.getNowDate());
        return appUserMapper.updateAppUser(appUser);
    }

    /**
     * 批量删除接口密钥管理
     * 
     * @param appUserIds 需要删除的接口密钥管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAppUserByAppUserIds(Long[] appUserIds)
    {
        appUserApiMapper.deleteAppUserApiByAppUserIds(appUserIds);
        return appUserMapper.deleteAppUserByAppUserIds(appUserIds);
    }

    /**
     * 删除接口密钥管理信息
     * 
     * @param appUserId 接口密钥管理主键
     * @return 结果
     */
    @Override
    public int deleteAppUserByAppUserId(Long appUserId)
    {
        return appUserMapper.deleteAppUserByAppUserId(appUserId);
    }

    @Override
    public void insertUserApiAuth(Long appUserId, String[] apiIds) {
        appUserApiMapper.deleteUserApiByAppUserId(appUserId);
        insertAppUserApi(appUserId, apiIds);
    }

    private void insertAppUserApi(Long appUserId, String[] apiIds) {

        if (StringUtils.isNotEmpty(apiIds))
        {
            List<AppUserApi> list = new ArrayList<AppUserApi>(apiIds.length);
            for (String apiId : apiIds)
            {
                AppUserApi aua = new AppUserApi();
                aua.setAppUserId(appUserId);
                aua.setApiId(apiId);
                list.add(aua);
            }
            appUserApiMapper.batchAppUserApi(list);
        }
    }

}
